<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="注册手机号" prop="mobilePhone" label-width="100">
        <el-input
          v-model="queryParams.mobilePhone"
          placeholder="请输入注册手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间" prop="registerTime">
        <el-date-picker clearable
          v-model="queryParams.registerTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择注册时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="主体类型" prop="mainType">
        <el-select v-model="queryParams.mainType" placeholder="请选择主体类型" cle arable style="width: 200px">
          <el-option
              v-for="dict in main_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input
          v-model="queryParams.legalPersonName"
          placeholder="请输入法人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有营业执照 " prop="businessFlag" label-width="120">
        <el-select

            v-model="queryParams.businessFlag"
            placeholder="是否有营业执照"
            clearable
            style="width: 200px"
        >
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="地区">
        <AreaSelect :model="areaModel" @update:model="updateAreaModel" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['nongjt:perfect:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nongjt:perfect:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nongjt:perfect:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['nongjt:perfect:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="perfectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="用户注册表ID" align="center" prop="userId" />
      <el-table-column label="注册手机号" align="center" prop="mobilePhone"  />
      <el-table-column label="注册时间" align="center" prop="registerTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.registerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主体名称" align="center" prop="companyName" />
      <el-table-column label="法人姓名" align="center" prop="legalPersonName" />
      <el-table-column label="是否有营业执照 " align="center" prop="businessFlag">
        <template #default="scope">
          <el-tag :type="scope.row.businessFlag === 1 ? 'success' : 'danger'">
            {{ scope.row.businessFlag === 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="营业执照" align="center" prop="businessLicenseUrl" >
        <template #default="scope">
          <image-preview :src="scope.row.businessLicenseUrl"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="认证资格" align="center" prop="certificationQualificationUrl" >
        <template #default="scope">
          <image-preview :src="scope.row.certificationQualificationUrl"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="品牌logo" align="center" prop="logoUrl" >
        <template #default="scope">
          <image-preview :src="scope.row.logoUrl"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="历史签名图片" align="center" prop="signUrl" >
        <template #default="scope">
          <image-preview :src="scope.row.signUrl"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="area" />
      <el-table-column label="乡镇/街道" align="center" prop="country" />
      <el-table-column label="详细地址" align="center" prop="detailAddress" />
      <el-table-column label="地址编码" align="center" prop="addressCode" />
      <el-table-column label="生产类型" align="center" prop="produceType" />
      <el-table-column label="主体类型" align="center" prop="mainType">
        <template #default="scope">
          <dict-tag :options="main_type" :value="scope.row.mainType"/>
        </template>
      </el-table-column>
      <el-table-column label="主体身份" align="center" prop="subjectIdentity" />
      <el-table-column label="行业类型" align="center" prop="industryType" />
      <el-table-column label="联系电话" align="center" prop="phoneNumber" />
      <el-table-column label="统一社会信用代码" align="center" prop="creditCode" />
      <el-table-column label="证件号码" align="center" prop="idNumber" />
      <el-table-column label="注册资金" align="center" prop="registerMoney" />
      <el-table-column label="照片墙" align="center" prop="photoWall" >
        <template #default="scope">
          <image-preview :src="scope.row.photoWall"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="照片墙介绍" align="center" prop="photoMsg" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="企业成立时间" align="center" prop="foundTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.foundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="examineStatus" width="100">
        <template #default="scope">
          <span v-if="scope.row.examineStatus === '2' || scope.row.examineStatus === 2">审核通过</span>
          <span v-else-if="scope.row.examineStatus === '1' || scope.row.examineStatus === 1">审核不通过</span>
          <span v-else-if="scope.row.examineStatus === '0' || scope.row.examineStatus === 0">待审核</span>
          <span v-else>{{ scope.row.examineStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核未通过原因" align="center" prop="examineMsg" />
      <el-table-column label="审核时间" align="center" prop="examineTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.examineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="省平台密码" align="center" prop="sptPassword" />
      <el-table-column label="省平台账号" align="center" prop="sptLoginName" />
      <el-table-column label="是否有省平台权限 " align="center" prop="sptAuth">
        <template #default="scope">
          <span v-if="scope.row.sptAuth === '1' || scope.row.sptAuth === 1">是</span>
          <span v-else-if="scope.row.sptAuth === '0' || scope.row.sptAuth === 0">否</span>
          <span v-else>{{ scope.row.sptAuth }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否同步到成都市 " align="center" prop="isSync">
        <template #default="scope">
          <span v-if="scope.row.isSync === '1' || scope.row.isSync === 1 || scope.row.isSync === '2' || scope.row.isSync === 2">是</span>
          <span v-else-if="scope.row.isSync === '0' || scope.row.isSync === 0">否</span>
          <span v-else>{{ scope.row.isSync }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否打印合格证抬头" align="center" prop="certificateTitleFlag">
        <template #default="scope">
          <span v-if="scope.row.certificateTitleFlag === '1' || scope.row.certificateTitleFlag === 1">是</span>
          <span v-else-if="scope.row.certificateTitleFlag === '0' || scope.row.certificateTitleFlag === 0">否</span>
          <span v-else>{{ scope.row.certificateTitleFlag }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdateCheck(scope.row)" v-hasPermi="['nongjt:perfect:edit']">审核</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nongjt:perfect:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nongjt:perfect:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户完善信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="perfectRef" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="用户注册表ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户注册表ID" />
        </el-form-item>
        <el-form-item label="注册手机号" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入注册手机号" />
        </el-form-item>
        <el-form-item label="注册时间" prop="registerTime" >
          <el-date-picker clearable
            v-model="form.registerTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="主体名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入主体名称" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="legalPersonName">
          <el-input v-model="form.legalPersonName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="是否有营业执照" prop="businessFlag">
          <el-select
              v-model="form.businessFlag"
              placeholder="是否有营业执照"
              clearable
              style="width: 200px"
          >
            <el-option label="否" :value="0" />
            <el-option label="是" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicenseUrl">
          <image-upload v-model="form.businessLicenseUrl" />
        </el-form-item>
        <el-form-item label="认证资质" prop="certificationQualificationUrl">
          <image-upload v-model="form.certificationQualificationUrl" />
        </el-form-item>
        <el-form-item label="品牌logo" prop="logoUrl">
          <image-upload v-model="form.logoUrl"  />
        </el-form-item>
        <el-form-item label="历史签名图片" prop="signUrl">
          <image-upload v-model="form.signUrl" />
        </el-form-item>
        <el-form-item label="地区">
          <AreaSelect :model="areaModel" @update:model="updateAreaModel" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="form.detailAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="地址编码" prop="addressCode">
          <el-input v-model="form.addressCode" placeholder="请输入地址编码" />
        </el-form-item>
        <el-form-item label="主体类型" prop="mainType">
          <el-select v-model="form.mainType" placeholder="请选择主体类型">
            <el-option
                v-for="dict in main_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主体身份" prop="subjectIdentity">
          <el-input v-model="form.subjectIdentity" placeholder="请输入主体身份" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="证件号码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="注册资金" prop="registerMoney">
          <el-input v-model="form.registerMoney" placeholder="请输入注册资金" />
        </el-form-item>
        <el-form-item label="照片墙" prop="photoWall">
          <image-upload v-model="form.photoWall" placeholder="请输入照片墙" />
        </el-form-item>
        <el-form-item label="照片墙介绍" prop="photoMsg">
          <el-input v-model="form.photoMsg" placeholder="请输入照片墙介绍" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="企业成立时间" prop="foundTime">
          <el-date-picker clearable
            v-model="form.foundTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择企业成立时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="省平台密码" prop="sptPassword">
          <el-input v-model="form.sptPassword" placeholder="请输入省平台密码" />
        </el-form-item>
        <el-form-item label="省平台账号" prop="sptLoginName">
          <el-input v-model="form.sptLoginName" placeholder="请输入省平台账号" />
        </el-form-item>
        <el-form-item label="是否有省平台权限" prop="sptAuth">
          <el-select
              v-model="form.sptAuth"
              placeholder="请选择是否有省平台权限"
              clearable
              style="width: 200px"
          >
            <el-option label="否" :value="0" />
            <el-option label="是" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否同步到成都市" prop="isSync">
          <el-select
              v-model="form.isSync"
              placeholder="请选择是否同步到成都市"
              clearable
              style="width: 200px"
          >
            <el-option label="无需同步" :value="0" />
            <el-option label="需要同步" :value="1" />
            <el-option label="已同步" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否打印合格证抬头" prop="certificateTitleFlag">
          <el-select
              v-model="form.certificateTitleFlag"
              placeholder="请选择是否打印合格证抬头"
              clearable
              style="width: 200px"
          >
            <el-option label="否" :value="0" />
            <el-option label="是" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="删除标记 " prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记 " />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog :title="checkTitle" v-model="checkOpen" width="500px" append-to-body>
      <el-form ref="checkRef" :model="checkForm" :rules="checkRules" label-width="120px">
        <el-form-item label="主体名称">
          <el-input v-model="checkForm.companyName" disabled />
        </el-form-item>
        <el-form-item label="法人姓名">
          <el-input v-model="checkForm.legalPersonName" disabled />
        </el-form-item>
        <el-form-item label="审核状态" prop="examineStatus">
          <el-select v-model="checkForm.examineStatus" placeholder="请选择审核状态" style="width: 100%">
            <el-option label="审核通过" value="2"></el-option>
            <el-option label="审核不通过" value="1"></el-option>
            <el-option label="待审核" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="examineMsg">
          <el-input
              v-model="checkForm.examineMsg"
              type="textarea"
              placeholder="请输入审核意见"
              :rows="4"
          />
        </el-form-item>
        <el-form-item label="审核时间" prop="examineTime">
          <el-date-picker clearable
                          v-model="checkForm.examineTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择审核时间"          style="width: 100%">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitCheckForm">确 定</el-button>
          <el-button @click="cancelCheck">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Perfect">
import { listPerfect, getPerfect, delPerfect, addPerfect, updatePerfect } from "@/api/nongjt/perfect"
import AreaSelect from "@/components/AreaSelect.vue"
const { proxy } = getCurrentInstance()

const { main_type } = proxy.useDict('main_type')
const checkOpen = ref(false)
const checkTitle = ref("")
const checkRef = ref()

const checkForm = ref({
  id: null,
  companyName: null,
  legalPersonName: null,
  examineStatus: null,
  examineMsg: null
})

const checkRules = ref({
  examineStatus: [
    { required: true, message: "审核状态不能为空", trigger: "change" }
  ]
})

const perfectList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    mobilePhone: null,
    registerTime: null,
    companyName: null,
    legalPersonName: null,
    businessFlag: null,
    mainType: null,
    phoneNumber: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询用户完善信息列表 */
function getList() {
  loading.value = true
  listPerfect(queryParams.value).then(response => {
    perfectList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    mobilePhone: null,
    registerTime: null,
    companyName: null,
    legalPersonName: null,
    businessFlag: null,
    businessLicenseUrl: null,
    certificationQualificationUrl: null,
    province: null,
    city: null,
    area: null,
    country: null,
    detailAddress: null,
    addressCode: null,
    produceType: null,
    mainType: null,
    subjectIdentity: null,
    industryType: null,
    phoneNumber: null,
    creditCode: null,
    idNumber: null,
    registerMoney: null,
    logoUrl: null,
    photoWall: null,
    photoMsg: null,
    longitude: null,
    latitude: null,
    foundTime: null,
    examineStatus: null,
    examineMsg: null,
    examineTime: null,
    sptPassword: null,
    sptLoginName: null,
    sptAuth: null,
    isSync: null,
    certificateTitleFlag: null,
    signUrl: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("perfectRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加用户完善信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPerfect(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改用户完善信息"
  })
}

function handleUpdateCheck(row) {
  checkForm.value = {
    id: row.id,
    companyName: row.companyName,
    legalPersonName: row.legalPersonName,
    examineStatus: row.examineStatus?.toString() || '',
    examineMsg: row.examineMsg || ''
  }
  checkOpen.value = true
  checkTitle.value = "用户信息审核"
}

// 提交审核表单
function submitCheckForm() {
  proxy.$refs["checkRef"].validate(valid => {
    if (valid) {
      const updateData = {
        id: checkForm.value.id,
        examineStatus: checkForm.value.examineStatus,
        examineMsg: checkForm.value.examineMsg,
        examineTime: new Date()
      }

      updatePerfect(updateData).then(response => {
        proxy.$modal.msgSuccess("审核成功")
        checkOpen.value = false
        getList()
      })
    }
  })
}

// 取消审核
function cancelCheck() {
  checkOpen.value = false
  proxy.resetForm("checkRef")
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["perfectRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePerfect(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPerfect(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除用户完善信息编号为"' + _ids + '"的数据项？').then(function() {
    return delPerfect(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

const areaModel = computed(() => {
  return {
    province: queryParams.value.province,
    city: queryParams.value.city,
    area: queryParams.value.area,
    country: queryParams.value.country
  }
})
//更新地区模型
function updateAreaModel(model) {
  queryParams.value.province = model.province
  queryParams.value.city = model.city
  queryParams.value.area = model.area
  queryParams.value.country = model.country
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('nongjt/perfect/export', {
    ...queryParams.value
  }, `perfect_${new Date().getTime()}.xlsx`)
}
getList()
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.mb8 {
  margin-bottom: 20px;
}

.mb8 :deep(.el-card) {
  margin-bottom: 15px;
}

/* 表格样式优化 */
:deep(.el-table) {
  margin-top: 15px;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
}

/* 表单样式优化 */
:deep(.el-form-item) {
  margin-bottom: 18px;
}

:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-dialog__header) {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

:deep(.el-dialog__footer) {
  padding: 15px 20px;
  border-top: 1px solid #eee;
}

/* 按钮样式优化 */
:deep(.el-button) {
  border-radius: 4px;
}

/* 分页样式优化 */
.pagination-container {
  margin-top: 20px;
  text-align: center;
}

/* 查询条件区域样式 */
.query-form {
  background-color: #f9f9f9;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 15px;
}

/* 状态标签样式 */
.status-tag {
  padding: 4px 8px;
  border-radius: 2px;
  font-size: 12px;
}
</style>
